package com.iotechn.unimall.data.util;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import lombok.Getter;
import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.Hashtable;

/**
 * @Author:chengchangjiang
 * @Description: 二维码Util类
 * @Date:Created in 9:38 2021-09-03
 */
@Component
public class QRCodeUtil implements InitializingBean {
    @Getter
    @Value("https://taohaoliang.oss-cn-beijing.aliyuncs.com/")
    private String baseUrl;
    @Getter
    @Value("taohaoliang")
    private String bucket;
    @Autowired
    private OSSClient ossClient;
    @Getter
    @Value("oss-cn-beijing.aliyuncs.com")
    private String endpoint;

    private String host;
    @Override
    public void afterPropertiesSet() throws Exception {
        host = "http://" + bucket + "." + endpoint;
    }

    /***
     * 生成二维码图片并上传OSS
     * @param text 二维码内容
     * @param width 宽度
     * @param height 高度
     * @param type 类型
     * @param name 图片名字
     * @return
     */
    public  String generateQRCodeImage(String text, int width, int height,String type,String name){
        try {
            QRCodeWriter qrCodeWriter = new QRCodeWriter();
            Hashtable<EncodeHintType, Object> hints = new Hashtable();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = qrCodeWriter.encode(type+":"+text, BarcodeFormat.QR_CODE, width, height,hints);

            File file = new File( new String(("templates" + File.separator + name+".png").getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8));
            if(!file.exists()){
                boolean flag = file.mkdirs();
            }
            Path path = FileSystems.getDefault().getPath(file.getAbsoluteFile().getPath());

            MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);

            FileInputStream fileInputStream = new FileInputStream(file.getAbsoluteFile());

            MultipartFile multipartFile = new MockMultipartFile( ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
            String result = upload(multipartFile,name);
            file.delete();
            return result;
        }
        catch (Exception e){
            System.out.println("二维码异常："+e.toString());
            return "error";
        }
    }
    /**
     * 后台通过服务器间接传文件
     * @param file
     * @return
     * @throws IOException
     */
    public  String upload(MultipartFile file, String sendCarNo) throws IOException {
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(file.getSize());
        objectMetadata.setContentType(file.getContentType());
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, "QRCode/" +sendCarNo+".png", file.getInputStream(), objectMetadata);
        ossClient.putObject(putObjectRequest);
        return baseUrl + "QRCode/" + sendCarNo+".png";
    }
}
